Qt.include("Storage.js");

WorkerScript.onMessage = function(message) {
    doTypeConnect(message)
}

function doTypeConnect(message) {
    var xhr = new XMLHttpRequest();
    var url = "http://coscup.org/2011/api/program/types/"

    message.model.clear();
    WorkerScript.sendMessage({"result": "PROGRESS"});

    xhr.open("GET", url);
    xhr.onreadystatechange = function() {
        try {
            if (xhr.readyState == XMLHttpRequest.DONE) {
                var response = JSON.parse(xhr.responseText);
                //WorkerScript.sendMessage({"result": "PROGRESS"});
                for (var i in response) {
                    var jsonData = response[i];
                    if (i == 0)
                        continue;

                    message.model.append({
                        type: jsonData
                    })

                }
            }
            message.model.sync();
            WorkerScript.sendMessage({"result": "SUCCESS"});
        } catch (err) {
            return -1;
            //ERROR
        }
    }
    xhr.send();
}

